Option Strict On
Option Explicit On 

Public Class FTextBox
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents btnApply As System.Windows.Forms.Button
    Friend WithEvents btnOk As System.Windows.Forms.Button
    Friend WithEvents btnCancel As System.Windows.Forms.Button
    Friend WithEvents UFrame1 As Avand.UFrame
    Friend WithEvents Panel1 As System.Windows.Forms.Panel
    Friend WithEvents ULabel1 As Avand.ULabel
    Friend WithEvents UMember1 As Avand.UMember
    Friend WithEvents UTextBox1 As Avand.UTextBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Dim resources As System.ComponentModel.ComponentResourceManager = New System.ComponentModel.ComponentResourceManager(GetType(FTextBox))
        Me.btnApply = New System.Windows.Forms.Button
        Me.btnOk = New System.Windows.Forms.Button
        Me.btnCancel = New System.Windows.Forms.Button
        Me.UFrame1 = New Avand.UFrame
        Me.Panel1 = New System.Windows.Forms.Panel
        Me.ULabel1 = New Avand.ULabel
        Me.UTextBox1 = New Avand.UTextBox
        Me.UMember1 = New Avand.UMember
        Me.Panel1.SuspendLayout()
        Me.SuspendLayout()
        '
        'btnApply
        '
        resources.ApplyResources(Me.btnApply, "btnApply")
        Me.btnApply.Name = "btnApply"
        '
        'btnOk
        '
        resources.ApplyResources(Me.btnOk, "btnOk")
        Me.btnOk.Name = "btnOk"
        '
        'btnCancel
        '
        resources.ApplyResources(Me.btnCancel, "btnCancel")
        Me.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.btnCancel.Name = "btnCancel"
        '
        'UFrame1
        '
        resources.ApplyResources(Me.UFrame1, "UFrame1")
        Me.UFrame1.Name = "UFrame1"
        '
        'Panel1
        '
        resources.ApplyResources(Me.Panel1, "Panel1")
        Me.Panel1.Controls.Add(Me.ULabel1)
        Me.Panel1.Controls.Add(Me.UTextBox1)
        Me.Panel1.Controls.Add(Me.UMember1)
        Me.Panel1.Controls.Add(Me.UFrame1)
        Me.Panel1.Name = "Panel1"
        '
        'ULabel1
        '
        resources.ApplyResources(Me.ULabel1, "ULabel1")
        Me.ULabel1.Name = "ULabel1"
        '
        'UTextBox1
        '
        resources.ApplyResources(Me.UTextBox1, "UTextBox1")
        Me.UTextBox1.Name = "UTextBox1"
        '
        'UMember1
        '
        resources.ApplyResources(Me.UMember1, "UMember1")
        Me.UMember1.Name = "UMember1"
        '
        'FTextBox
        '
        Me.AcceptButton = Me.btnOk
        resources.ApplyResources(Me, "$this")
        Me.CancelButton = Me.btnCancel
        Me.Controls.Add(Me.Panel1)
        Me.Controls.Add(Me.btnApply)
        Me.Controls.Add(Me.btnOk)
        Me.Controls.Add(Me.btnCancel)
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog
        Me.MaximizeBox = False
        Me.MinimizeBox = False
        Me.Name = "FTextBox"
        Me.ShowInTaskbar = False
        Me.Panel1.ResumeLayout(False)
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private mChanged As Boolean
    Private mTextBox As Model10.TextBox
    Private mSection As Model10.Section

    Public Event ValueChenged As EventHandler

    Private Sub FTextBox_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        AddHandler UMember1.ValueChanged, AddressOf OnValueChanged
        AddHandler UFrame1.ValueChanged, AddressOf OnValueChanged
        AddHandler ULabel1.ValueChanged, AddressOf OnValueChanged
        AddHandler UTextBox1.ValueChanged, AddressOf OnValueChanged
    End Sub
    Private Sub btnApply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnApply.Click
        Application.DoEvents()
        Cursor.Current = Cursors.WaitCursor
        '----------------------------------
        Try
            Dim txt As New Model10.TextBox
            UMember1.Write(txt) : UFrame1.Write(txt) : ULabel1.Write(txt) : UTextBox1.Write(txt)
            If mTextBox Is Nothing Then
                If Not mSection Is Nothing Then mSection.Members.Add(txt, 0, 0)
                mTextBox = txt
            Else
                UMember1.Write(mTextBox) : UFrame1.Write(mTextBox) : ULabel1.Write(mTextBox) : UTextBox1.Write(mTextBox)
            End If
            txt = Nothing

            mChanged = True
            btnApply.Enabled = False
            RaiseEvent ValueChenged(Me, New EventArgs)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        '----------------------------------
        Cursor.Current = Cursors.Default
    End Sub
    Private Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click
        btnApply.PerformClick()
        If Not btnApply.Enabled Then DialogResult = DialogResult.OK
    End Sub

    Protected Sub OnValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        btnApply.Enabled = True
        RaiseEvent ValueChenged(Me, New EventArgs)
    End Sub

    Public Property Changed() As Boolean
        Get
            Return mChanged
        End Get
        Set(ByVal Value As Boolean)
            mChanged = Value
        End Set
    End Property
    Public Property TextBox() As Model10.TextBox
        Get
            Return mTextBox
        End Get
        Set(ByVal Value As Model10.TextBox)
            mTextBox = Value
        End Set
    End Property
    Public Property Section() As Model10.Section
        Get
            Return mSection
        End Get
        Set(ByVal Value As Model10.Section)
            mSection = Value
        End Set
    End Property

    Public Sub Fill()
        UMember1.Prepare() : UFrame1.Prepare() : ULabel1.Prepare(Section)
        If mTextBox Is Nothing Then
            Dim txt As New Model10.TextBox
            UMember1.Read(txt) : UFrame1.Read(txt) : ULabel1.Read(txt, mSection) : UTextBox1.Read(txt)
            txt = Nothing

            UMember1.txtName.Text = mSection.GetNewMemberName([Global].GetSystemResource("Member.TextBox"))

            btnApply.Enabled = True
        Else
            UMember1.Read(mTextBox) : UFrame1.Read(mTextBox) : ULabel1.Read(mTextBox, mSection) : UTextBox1.Read(mTextBox)

            btnApply.Enabled = False
        End If
        mChanged = False
    End Sub
    Public Sub New(ByVal section As Model10.Section, ByVal textBox As Model10.TextBox)
        MyBase.New()
        InitializeComponent()

        mTextBox = TextBox
        mSection = section
        Fill()
    End Sub
End Class
